<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use app\modules\Cruise\models\Cruise;

$this->title = Yii::t('app.cruise', 'Orders');
$this->params['breadcrumbs'][] = $this->title;

$cruise = Cruise::find()->where(['status'=>1])->all(); 


//名单模板
$template = yii::$app->params['template'];
?>
<div class="product-index">

    <h2 class="page-header"><small><?= Html::encode($this->title) ?></small>
		<p class="pull-right">
            
            <?= Html::a('下载价格', "/product/product/product-price",['title'=>'下载价格', 'class'=>'btn btn-success', 'target'=>'_blank']) ?>

			<?= Html::a('订单列表', "/order/order/my-index",['class'=>'btn btn-primary', 'target'=>'_blank']) ?>
            <?= Html::a('打包下载word', "/order/word/pack",['title' =>'打包下载word', 'class'=>'btn btn-warning', 'target'=>'_blank']) ?>
        </p>
    </h2>

	<!-- </?PHP Pjax::begin(['id'=>'product', 'timeout'=>3000, 'linkSelector'=>'#product a[data-sort], #product a[data-page]']);?> -->
    <p><?php echo $this->render('_list', ['model' => $searchModel]); ?></p>
	
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        //'filterModel' => $searchModel,
        'layout'  =>  "{items}\n{pager}",
        'columns' => [
            [
                'attribute' => 'name',
                'value' => function ($model) {
                       $url =  sprintf("/front/default/lines?id=%d", $model->id);
                       return Html::a($model->name, $url, ['title' =>$model->name, 'target'=>'_blank']);
                    },
                'format'=> 'raw',   
            ],
			'code',
            'go_time:date',
            //'return_time:date',
            [
                'attribute' => 'type',
                'value' => function ($model) {
                        $t1 = Yii::$app->params['product_type'][$model->type];
						$t2 = Yii::$app->params['product_stock'][$model->stock];
						$t3 = empty($model->date_limit) ? '未设置占位期限' : '占位有效期: '.$model->date_limit .'天';
						return  '<small>'. $t1 . '<br>' . $t2 . '<br>' . $t3 . '</small>';
                    },
                'format'=> 'raw',   
            ],
            [
                'attribute' => 'status',
                'filter' =>  Yii::$app->params['status'],
                'value' => function ($model) {
                        return Yii::$app->params['status'][$model->status];
                    },
            ],
            [
                'label'=>'金额',
                'format'=>'raw',
                'value' => function($model){
					//print_r(count($model->financePrice));
						
					return "<small>订单数量 : " .$model->allOrderNum . '<br>' . "舱房数 : ". $model->allRoomNum ." / 人数 : ".$model->allCustomerNum."<br>"."总金额 : " .$model->allOrderPrice . "<br>实收 : ". $model->financeSuccess . "</small>";
                }
            ],
            [
                'label'=>'库存',
                'format'=>'raw',
                'value' => function($model){
					$a = Html::a('详情','/order/order/stock?product_id='.$model->id, ['data-toggle'=>'modal', 'data-target'=>'.stock-modal']);
					if($model->stock == 1){
						$n = $model->stockAll-$model->surplusStock;
                    	return sprintf("总数 : %s <br> 剩余 : %s <br> %s", $model->stockAll, $n ,$a);
					}else{
						$n = $model->stockAll2-$model->surplusStock;
                    	return sprintf("总数 : %s <br> 剩余 : %s <br> %s", $model->stockAll2,$n, $a);
					}
                }
            ],
            [
                'label'=>'查看订单',
                'format'=>'raw',
                'value' => function($model){
                    $url  = sprintf("/order/order/index?product_id=%d", $model->id);
                    $url2 = sprintf("/order/order/log?id=%d&type=2", $model->id);
                    $url3 = sprintf("/order/order/commiss-list?product_id=%d", $model->id);

                    $str  =  Html::a('<i class="fa fa-eye"></i>',$url,['title' =>'订单列表','target'=>'_blank']).'&nbsp;&nbsp;&nbsp;&nbsp;'; 
					$str .=  Html::a('<i class="fa fa-calendar-check-o"></i>',$url2,['title' =>'操作记录', 'data-toggle'=>'modal', 'data-target'=>'.stock-modal']).'&nbsp;&nbsp;&nbsp;&nbsp;'; 
                    $str .=  Html::a('<i class="fa fa-gg"></i>',$url3,['title' =>'后返列表','target'=>'_blank']); 
                    return $str;
                }
            ],
            [
                'label'=>'下载',
                'format'=>'raw',
                'value' => function($model){
                    $url3 = sprintf("/order/order/pack?product_id=%d", $model->id);
                    $url7 = sprintf("/order/order/card-pack?product_id=%d", $model->id);
                    $url6= sprintf("/order/word/sell-excel?product_id=%d", $model->id);
                    $url8= sprintf("/order/word/report-excel?product_id=%d", $model->id);
                    $xdoc = sprintf("%s/product/product/word?id=%d&type=1",yii::$app->request->getHostInfo(),$model->id);
                    $xdoc_2 = sprintf("%s/product/product/word?id=%d&type=2",yii::$app->request->getHostInfo(),$model->id);
                    $_xdoc = urlencode($xdoc);
                    $_xdoc_2 = urlencode($xdoc_2);
                    $url4 = sprintf("http://www.xdocin.com/xdoc?_func=run&_format=doc&_xdoc=%s&_filename=%s(%s)(同业价格)",$_xdoc,$model->cruise->name,$model->go_time);
                    $url5 = sprintf("http://www.xdocin.com/xdoc?_func=run&_format=doc&_xdoc=%s&_filename=%s(%s)(直客价格)",$_xdoc_2,$model->cruise->name,$model->go_time);

                    $xdoc_3 = sprintf("%s/product/product/neword?id=%d&type=1",yii::$app->request->getHostInfo(),$model->id);
                    $xdoc_4 = sprintf("%s/product/product/neword?id=%d&type=2",yii::$app->request->getHostInfo(),$model->id);
                    $_xdoc_3 = urlencode($xdoc_3);
                    $_xdoc_4 = urlencode($xdoc_4);

                    $url14 = sprintf("http://www.xdocin.com/xdoc?_func=run&_format=doc&_xdoc=%s&_filename=%s(%s)(同业价格)",$_xdoc_3,$model->cruise->name,$model->go_time);
                    $url15 = sprintf("http://www.xdocin.com/xdoc?_func=run&_format=doc&_xdoc=%s&_filename=%s(%s)(直客价格)",$_xdoc_4,$model->cruise->name,$model->go_time);

                    $str = Html::a('<i class="fa fa-file-word-o"></i>',$url4,['title' => '生成WORD包含同业价格', 'target'=>'_blank']).'&nbsp;&nbsp;';
                    $str .= Html::a('<i class="fa fa-file-o"></i>',$url5,['title' => '生成WORD不包含同业价格', 'target'=>'_blank']).'&nbsp;&nbsp;';
                    $str .= Html::a('<i class="fa fa-file-excel-o"></i>',$url6,['title' => '下载航期订单列表', 'target'=>'_blank']).'&nbsp;&nbsp;';

                     $str .= Html::a('<i class="fa fa-file-picture-o"></i>',$url14,['title' => '生成最新WORD包含同业价格', 'target'=>'_blank']).'&nbsp;&nbsp;';
                    $str .= Html::a('<i class="fa fa-file-powerpoint-o"></i>',$url15,['title' => '生成最新WORD不包含同业价格', 'target'=>'_blank']).'&nbsp;&nbsp;';
                    
                    return $str;
                }
            ],

        ],
    ]); ?>
	<!-- </?php Pjax::end(); ?> -->
</div>
<!-- 
	/*
	    $url = sprintf("/order/type/index?product_id=%d", $model->id);
        $url2 = sprintf("/order/room/list?product_id=%d", $model->id);

		$str  = Html::a('<i class="fa fa-bus"></i>',$url,['title' =>'附加费配置','target'=>'_blank']).'&nbsp;&nbsp;'; 
		$str .= Html::a('<i class="fa fa-home"></i>',$url2,['title' =>'分房号','target'=>'_blank']).'&nbsp;&nbsp;'; 
	 */
-->

<div class="modal fade stock-modal"   role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document" style="width:740px;">
      <div class="modal-content">
	 		<!-- 嵌入内容 --> 
	  </div>
  </div>
</div>


<!-- 选择游轮模板名单 -->
<div class="modal fade product-modal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document">
      <div class="modal-content">
           
            <input type="hidden" name="product_id" id="product_id" value="">
            <ul class="list-group">
                <li class="list-group-item" style="background-color:#666;color:#FFF;">选择名单模板</li>
                <?php foreach($template as $k=>$v){ ?>
                <li class="list-group-item"><label><input type="radio" name="template_id" value="<?= $k?>"> <?= $v ?></label></li>
                <?php } ?>
                <li class="list-group-item"><button class="btn btn-primary" id="create">确定 </button></li>
            </ul>
            
      </div>
  </div>
</div>

<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">
// 清除模态框缓存
$(".stock-modal").on("hidden.bs.modal", function() {
	$(this).removeData("bs.modal");
});

$(".product-modal").on("hidden.bs.modal", function() {
    $(this).removeData("bs.modal");
});

$(".create_excel").click(function(){
     $('.product-modal').modal('show');
     var product_id = $(this).attr("data-product-id");
     $("#product_id").val(product_id);
});

$("#create").click(function(){
    var product_id = $("#product_id").val();
    var template_id = $("input[name='template_id']:checked").val();
    if(!template_id){
        alert("未选择名单模板");
        return false;
    }
    var url = "/order/customer/create-excel?product_id="+product_id+"&template_id="+template_id;
    window.open(url);

});

</script>
<?php app\components\JsBlock::end() ?>   
